<template>
  <div class="login_container">
    <!-- <img class="clo_img" src="../assets/login_close.jpg" alt=""> -->
    <div class="logo fff txt_cent">
      <img src="../assets/logo.png" alt="" />
      <div>
        <div class="title">欣心</div>
        <div class="title">寻找不一样的你</div>
      </div>
    </div>
    <div class="left common_no_drag">
      <div
        class="btn txt_cent curs_poin"
        :class="{ active_btn: current === 1 }"
        @click="loginStatus(1)"
      >
        <img src="../assets/video_ico.png" alt="" />
        <span class="fff">主播</span>
      </div>
      <div
        class="btn txt_cent curs_poin"
        :class="{ active_btn: current === 2 }"
        @click="loginStatus(2)"
      >
        <img src="../assets/auto_ico.png" alt="" />
        <span class="fff">主持</span>
      </div>
    </div>
    <div class="right flex">
      <div class="title">欢迎您，主播~</div>
      <el-form
        ref="ruleFormRef"
        :rules="rules"
        :model="form"
        label-width="auto"
        style="max-width: 600px"
      >
        <div class="inp_form row_inp">
          <el-form-item prop="phone">
            <el-input placeholder="请输入手机号" v-model.trim="form.phone" />
          </el-form-item>
        </div>
        <div class="inp_form2 inp_form fff row_inp">
          <el-form-item prop="verifyCode">
            <el-input
              placeholder="请输入验证码"
              v-model.trim="form.verifyCode"
            />
          </el-form-item>
          <el-button
            :disabled="true"
            style="flex: 1; margin-left: 15px; background: #32343a"
            class="fff"
            v-if="second > 1"
            >{{ second }}s</el-button
          >
          <el-button v-else class="btn_verify fff" @click="handlerVerifyCode"
            >获取验证码</el-button
          >
        </div>

        <div class="record fff">
          <el-form-item>
            <el-checkbox value="Online activities" name="type">
              5天内自动登录
            </el-checkbox>
          </el-form-item>
        </div>
        <div class="read_http fff">
          <el-form-item>
            <el-checkbox
              class="custom-checkbox fs_11"
              label="Option 1"
              :value="form.checkCircle"
            >
              我已阅读并同意欣心
            </el-checkbox>
            <span class="fs_11 cur_poi" @click="handleAgreement">《用户注册协议》</span>
            和<span class="fs_11 cur_poi" @click="handlePolicy">《隐私政策》</span>
          </el-form-item>
        </div>
      </el-form>

      <div @click="linkToHome" class="common_no_drag btn_log inp_form tc fff">登录</div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { useCounterStore } from "../store";
const store = useCounterStore();
import { getSendsms, getDxlogin, $TipMessage } from "../http";
const { ipcRenderer } = require("electron");
import { useRouter } from "vue-router";
const info = ref([]);
const second = ref(-1);
let timer = null;
const router = useRouter();
const isDisabl = ref(false);
const handleMinimize = () => {
  ipcRenderer.send("login_minimize");
};
const handleClose = () => {
  ipcRenderer.send("windows_close");
};
const validatePhone = (rule, val, callback) => {
  if (!val) {
    return callback(new Error("请输入手机号"));
  }
  function isPhone(phone) {
    const regex = /^1[3-9]\d{9}$/;
    return regex.test(phone);
  }
  if (!isPhone(val)) {
    return callback(new Error("请输入正确的手机号"));
  }
  return callback();
};
const loginStatus = (cur) => {
  current.value = cur;
};
const validateVerifyCode = (rule, val, callback) => {
  if (!val) {
    return callback(new Error("请输入验证码"));
  }
  return callback();
};
const rules = reactive({
  phone: [{ validator: validatePhone, trigger: "blur" }],
  verifyCode: [{ validator: validateVerifyCode, trigger: "blur" }],
});
const current = ref(1);
const ruleFormRef = ref(null);
const form = ref({ phone: "", verifyCode: "", checkCircle: false });
const handlerVerifyCode = () => {
  console.log("ruleFormRef.value", ruleFormRef.value);
  ruleFormRef.value.validateField("phone", (v) => {
    if (v) {
      getSendsms(form.value.phone).then((res) => {
        console.log("http-res", res);
        if (res.data.errcode === 0) {
          $TipMessage(res.data.data.res, "success");
          isDisabl.value = !isDisabl.value;
          //  $TipMessage(res.data.errmsg, "error");
          second.value = 60;
          timer = setInterval(() => {
            second.value -= 1;
            console.log("second.value", second.value);
            if (second.value <= 1) {
              console.log("22222");
              clearTimeout(timer);
            }
          }, 300);
        } else {
          $TipMessage(res.data.errmsg, "success");
        }
      });
    }
  });
};
const linkToHome = () => {
  ruleFormRef.value
    .validate()
    .then((res) => {
      //     console.log("登录res", res);
      // 登录接口
      getDxlogin(form.value.verifyCode, form.value.phone)
        .then((res) => {
          console.log("login res", res);
          if (res.data.errcode === 0) {
            info.value = res.data.data;
            console.log(" info.value ------------------login", info.value);
            store.loginer = info.value;
            localStorage.setItem("info", JSON.stringify(info.value));
            $TipMessage("登录成功", "suceess");
            ipcRenderer.send("login-success");
            router.push("/home");
          } else {
            $TipMessage(res.data.errmsg, "error");
          }
        })
        .catch((err) => {
          console.log("login err", err);
        });
    })
    .catch((err) => {
      for (const key in rules) {
        return $TipMessage(err[key][0].message, "error");
      }
    });
};
// 协议
const handleAgreement = () => {};
// 政策
const handlePolicy = () => {};
onMounted(() => {
  let info = JSON.parse(localStorage.getItem("info"));
  if (info?.jwt) {
    ipcRenderer.send("login-success");
    router.push("/home");
  }
});
</script>

<style lang="scss" scoped>
.common_no_drag{
  -webkit-app-region: no-drag;
}
.flex {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.txt_cent {
  display: flex;
  align-items: center;
  justify-content: center;
}
.tc {
  text-align: center;
}
.fff {
  color: #ffffff;
}
.logo {
  position: fixed;
  left: 24px;
  top: 32px;
  img {
    width: 61px;
    height: 61px;
    margin-right: 10px;
  }
  .title {
    font-family: Microsoft YaHei, Microsoft YaHei;
    font-weight: bold;
    font-size: 24px;
    color: #ffffff;
    text-align: left;
    font-style: normal;
    text-transform: none;
  }
  .title:last-child {
    font-size: 12px;
    color: #c3c3c4;
  }
}
.login_container {
  -webkit-app-region: drag;
  cursor: grab;
  user-select: none;
  // border: 5px solid red;
  position: relative;
  .clo_img {
    position: absolute;
    right: 20px;
    top: 20px;
    width: 20px;
    height: 20px;
    // img{
    //   width: 50px;
    //   height: 50px;
    // }
  }
  // border: 2px solid red;
  width: 100vw;
  height: 100vh;
  background: black;
  display: flex;
  .left {
    margin: auto 0;
    padding: 0 67px;
    box-sizing: border-box;

    .btn {
      width: 192px;
      padding: 24px 0;

      background: #343739;
      border-radius: 10px 10px 10px 10px;
      border: 1px solid #343739;
      img {
        width: 32px;
        height: 32px;
        margin-right: 22px;
      }
      span {
        display: inline-block;
        height: 100%;
      }
    }
    .btn:first-child {
      margin-bottom: 49px !important;
    }
    .active_btn {
      border: 1px solid #ff3395;
    }
  }
  .right {
    width: 356px;
    // border: 1px solid red;
    .menu_img {
      position: absolute;
      right: 15px;
      top: 15px;
      img {
        width: 25px;
        height: 25px;
      }
      img:first-child {
        margin-right: 15px;
      }
    }
    // :deep(.el-checkbox__input.is-checked .el-checkbox__inner:active){
    //     background:linear-gradient( 90deg, #FCCF52 0%, #FF418F 100%);
    // }
    :deep(.el-input__wrapper) {
      box-shadow: none;
      padding: 15px;
      box-sizing: border-box;
      background: #32343a;
      border-radius: 50px 50px 50px 50px;
    }
    :deep(.el-input__wrapper:active) {
      box-shadow: none;
      border: none;
    }

    .title {
      margin-bottom: 46px;
      // text-align: center;

      font-family: YS HelloFont BangBangTi, YS HelloFont BangBangTi;
      font-weight: 400;
      font-size: 34px;
      color: #ffffff;
      text-align: left;
      font-style: normal;
      text-transform: none;
    }

    .row_inp {
      margin: auto;
    }
    .inp_form {
      width: 356px;
      margin-bottom: 30px;
      :deep(.el-form-item__error) {
        padding-left: 20px;
        box-sizing: border-box;
      }
    }
    .inp_form2 {
      display: flex;
      justify-content: space-between;
      :deep(.el-button) {
        border: none;
        padding: 30px;
        background: #ff3395;
        border-radius: 50px 50px 50px 50px;
      }
    }
    .btn_log {
      padding: 18.5px 0;
      box-sizing: border-box;
      background: linear-gradient(90deg, #fccf52 0%, #ff418f 100%);
      border-radius: 50px 50px 50px 50px;
    }

    .record,
    .read_http {
      padding: 0 20px;
      box-sizing: border-box;
    }
    .read_http {
      font-family: Microsoft YaHei, Microsoft YaHei;
      font-weight: 400;
      text-align: left;
      font-style: normal;
      text-transform: none;
      :deep(.el-checkbox__inner) {
        border-radius: 50%;
      }
    }

    span {
      color: #ff3395;
    }
  }
}
</style>
